package LibraryDemo;

import org.omg.CORBA.PUBLIC_MEMBER;

import LibraryDemo.Dao.*;
import LibraryDemo.Entity.*;

public class BookManager {
	private IBookDao _dao;
	public BookManager(IBookDao dao){
		this._dao = dao;
	}
	
	public void deleteOneBook(int bookId)
			throws NullReferenceException{
		if (this._dao == null){
			throw new NullReferenceException("BookDao is null");
		}
		this._dao.deleteBook(bookId);
	}
	
	public void putOneBook(Book book)
			throws InvalidBookException, NullReferenceException { 
		validateBook(book);
		if (this._dao == null){
			throw new NullReferenceException("BookDao is null");
		}
		this._dao.persistBook(book);
		
	}
	
	public Book getOneBook(int bookId)
			throws InvalidBookException, NullReferenceException {
		if (this._dao == null){
			throw new NullReferenceException("BookDao is null");
		}
		Book book = this._dao.fetchBook(bookId);
		return book;
	}
	private boolean validateBook(Book book) 
			throws InvalidBookException{
		if (book.getBookId() <= 0)
			throw new InvalidBookException("BookId cannot less or equal to 0");
		if (book.getBookName() == null || book.getBookName().equals(""))
			throw new InvalidBookException("BookName cannot be null or empty");
		return true;
	}
}
